//
//  YBSecretaryXiangMuDetailViewController.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/8/28.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "YBSecretaryXiangMuDetailViewController.h"
#import "YBSecretaryListDetailInfoTableViewCell.h"
#import "LPSecretaryXiangMuRequestListViewController.h"
#import "LPSecretaryXiangMuResouceListViewController.h"
#import "YBRequestManager.h"
#define DETAILCELL @"YBSecretaryListDetailInfoTableViewCell"
@interface YBSecretaryXiangMuDetailViewController ()<UITableViewDelegate , UITableViewDataSource>
@property (nonatomic , strong) UITableView * mainTableView;
//总高度
@property (nonatomic , assign) CGFloat contentHeight;
//标题大小
@property (nonatomic , assign) CGSize titleSize;
//需求宽度
@property (nonatomic , assign) CGFloat needWidth;
//内容大小
@property (nonatomic , assign) CGSize detailInfoSize;

@property (nonatomic , assign) BOOL isRequestSuccess;

@property (nonatomic , strong) UIImageView * titleImageView;

@property (nonatomic , strong) UIView * noDataContentView;

@end

@implementation YBSecretaryXiangMuDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.contentHeight = 0.f;
    self.titleSize = CGSizeZero;
    self.isRequestSuccess = NO;
    self.contentHeight = 0.f;
    self.titlelabel.text = [NSString stringWithFormat:@"%@" , self.model.NAME];
    [self initNoDataContentView];
    [self ybInitDataSource];
}

-(void)initNoDataContentView{
    self.noDataContentView = [[UIView alloc]initWithFrame:CGRectMake(0, LP_TopHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - LP_TopHeight - ItemHeight * 88)];
    [self.view addSubview:self.noDataContentView];
    
}

-(void)ybInitDataSource{
    [YJProgressHUD showMessage:@"加载中..." duration:10 view:self.view];
    [YBRequestManager request_projectDetailWithProjectId:self.model.ID successBlock:^(id object) {
        NetWorkModel * netModel = [NetWorkModel yy_modelWithJSON:object];
        if ([netModel.STATE isEqualToString:@"20000"]) {
            NSDictionary * tempDic = object[@"INFO"];
            if(tempDic.count > 0){
                self.isRequestSuccess = YES;
                self.model = [LPSecretaryPrejectModel yy_modelWithJSON:tempDic];
                self.model.reqCnt = [NSString stringWithFormat:@"%@" , object[@"STATISTICS"][@"reqCnt"]];
                self.model.resCnt = [NSString stringWithFormat:@"%@" , object[@"STATISTICS"][@"resCnt"]];
                [self calculationModel];
                [self ybInitUserInterface];
            }
            else{
                [LYEmptyManager showNoDataWithView:self.mainTableView];
            }
        }
        [YJProgressHUD hide];
    } failBlock:^(id object) {
        [YJProgressHUD hide];
        if (!self.isRequestSuccess) {
            [LYEmptyManager showLoadFailWithView:self.mainTableView managerBlock:^{
                [self ybInitDataSource];
            }];
        }
    }];
}

//计算model相关属性
-(void)calculationModel{
    if ([self.model.reqCnt integerValue] == 0) {
        self.model.reqCnt = 0;
    }
    if ([self.model.resCnt integerValue] == 0) {
        self.model.resCnt = 0;
    }
    //计算头部高度
    NSString * titleSting = [NSString stringWithFormat:@"%@" , self.model.NAME];
    self.titleSize = [titleSting boundingRectWithSize:CGSizeMake([UIScreen zj_screenWith] - 48 * ItemWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15 * [UIScreen zj_textFontScale] weight:UIFontWeightHeavy]} context:nil].size;
    //计算需求宽度
    NSString * needSting = [NSString stringWithFormat:@"需求%@个" , self.model.reqCnt];
    self.needWidth = [needSting boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont titleTextFont]} context:nil].size.width;
    //计算内容高度
    NSString * detailInfoString = [NSString stringWithFormat:@"%@" , self.model.SUMMARY];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:ItemHeight * 10];
    self.detailInfoSize = [detailInfoString boundingRectWithSize:CGSizeMake([UIScreen zj_screenWith] - 48 * ItemWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont titleTextFont] , NSParagraphStyleAttributeName : paragraphStyle} context:nil].size;
    //总高度
    self.contentHeight = ItemHeight * 40 + self.titleSize.height + ItemHeight * 88 + self.detailInfoSize.height + 160 * ItemHeight;
}


-(void)ybInitUserInterface{
    [self.view addSubview:self.mainTableView];
    UIButton * needButton = [ToolClass createButtonWithFrame:CGRectMake(0, [UIScreen zj_screenHeight] - 88 * ItemHeight, [UIScreen zj_screenWith] / 2, 88 * ItemHeight) text:@"我有需求" textColor:[UIColor textBlackColor] imageName:@"woyouxuqiu" font:[UIFont titleTextFontSize] imageSize:50 * ItemHeight];
    needButton.tag = 1;
    [needButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton * assetButton = [ToolClass createButtonWithFrame:CGRectMake([UIScreen zj_screenWith] / 2, [UIScreen zj_screenHeight] - 88 * ItemHeight, [UIScreen zj_screenWith] / 2, 88 * ItemHeight) text:@"我有资源" textColor:[UIColor textBlackColor] imageName:@"tab_zhaoziyuan_h" font:[UIFont titleTextFontSize] imageSize:50 * ItemHeight];
    assetButton.tag = 2;
    [assetButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:needButton];
    [self.view addSubview:assetButton];
    
    UIView * shuxianView = [[UIView alloc]initWithFrame:CGRectMake([UIScreen zj_screenWith] / 2, [UIScreen zj_screenHeight] - 88 * ItemHeight, 1, 88 * ItemHeight)];
    shuxianView.backgroundColor = [UIColor zj_colorWithHexString:@"c9c9c9"];
    [self.view addSubview:shuxianView];
}


- (void)clickButton:(UIButton *)sender{
    if (sender.tag == 1) {
        LPSecretaryXiangMuResouceListViewController *vc = [LPSecretaryXiangMuResouceListViewController new];
        vc.model = self.model;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        LPSecretaryXiangMuRequestListViewController *vc = [LPSecretaryXiangMuRequestListViewController new];
        vc.model = self.model;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return ItemHeight * 416;
    }
    else{
        return self.contentHeight;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ImageCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ImageCell"];
            [cell.contentView addSubview:self.titleImageView];
            //底部灰色视图
            UIView * ybBottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 400 * ItemHeight, [UIScreen zj_screenWith] , 16 * ItemHeight)];
            ybBottomView.backgroundColor = [UIColor zj_colorWithHexString:@"c9c9c9"];
            [cell.contentView addSubview:ybBottomView];
        }
        [self.titleImageView sd_setImageWithURL:CREATEURL(self.model.IMG_filePath) placeholderImage:PlaceholderBIGImage];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else{
        YBSecretaryListDetailInfoTableViewCell * cell = [self.mainTableView dequeueReusableCellWithIdentifier:DETAILCELL forIndexPath:indexPath];
        cell.cellTitleLabel.text = [NSString stringWithFormat:@"%@" , self.model.NAME];
        cell.needNumberLabel.attributedText = [self getAttributedStringByNumberString:self.model.reqCnt titleString:@"需求"];
        cell.assetNumberLabel.attributedText = [self getAttributedStringByNumberString:self.model.resCnt titleString:@"资源"];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:ItemHeight * 10];
        NSString * detailInfoString = [NSString stringWithFormat:@"%@" , self.model.SUMMARY];
        NSMutableAttributedString * detailInfoAttributedString = [[NSMutableAttributedString alloc]initWithString:detailInfoString];
        [detailInfoAttributedString addAttribute:NSFontAttributeName value:[UIFont subTitleTextFont] range:NSMakeRange(0, detailInfoString.length)];
        [detailInfoAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, detailInfoString.length)];
        cell.cellDetailInfoLabel.attributedText = detailInfoAttributedString;
        cell.addressLabel.text =[NSString stringWithFormat:@"%@" , self.model.ADDRESS];
        [cell reloadCellSizeWithTitleHeight:self.titleSize needWidth:self.needWidth detailInfoHeight:self.detailInfoSize];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}


-(NSAttributedString *)getAttributedStringByNumberString:(NSString *)numberString titleString:(NSString *)titleString{
    NSMutableAttributedString * needAttributedString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@个" ,titleString , numberString]];
    [needAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor zj_colorWithHexString:@"1c81d3"] range:NSMakeRange(2, numberString.length)];
    [needAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15 * [UIScreen zj_textFontScale] weight:UIFontWeightHeavy] range:NSMakeRange(2, numberString.length)];
    return [needAttributedString copy];
}

-(UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, LP_TopHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - LP_TopHeight - ItemHeight * 88) style:UITableViewStylePlain];
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_mainTableView registerClass:[YBSecretaryListDetailInfoTableViewCell class] forCellReuseIdentifier:DETAILCELL];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.backgroundColor = [UIColor zj_colorWithHexString:@"f3f3f3"];
    }
    return _mainTableView;
}


-(UIImageView *)titleImageView
{
    if (!_titleImageView) {
        _titleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], ItemHeight * 400)];
    }
    return _titleImageView;
}


@end
